var t = require("../../../@babel/runtime/helpers/defineProperty"), a = getApp();

Page({
    data: {
        icon: !1,
        id: "",
        status: "1",
        poster: !1,
        share: !1,
        tipShow: !1,
        show: !1,
        isLoadMore: !0,
        page: 1,
        total_page: 0,
        list: [],
        activity_id: 0,
        activity_data: {},
        hand_goods_obj: {},
        hand_goods_id: 0,
        shareShow: !1,
        specsShow: !1,
        goodsPopupShow: !1,
        cartListShow: !1,
        time: 0,
        timeData: {},
        shareOptions: [ {
            name: "生成海报",
            icon: "poster"
        }, {
            name: "分享给好友",
            icon: "wechat",
            openType: "share"
        } ],
        goods_list: [],
        buyerListData: [],
        carList: [],
        goodsCartsTotal: {},
        language: {},
        posterShow: !1,
        cartsNum: 0,
        cartsTotal: 0,
        cart_ids: [],
        showChoose: !1,
        radioChoose: "",
        dispatch_types: []
    },
    onLoad: function(t) {
        if (this.setData({
            id: t.id
        }), t.scene) {
            var a, e = decodeURIComponent(t.scene);
            a = e.split(","), this.setData({
                id: a[0]
            }), console.log(a, e);
        }
        var s = wx.getStorageSync("langIndex");
        this.setData({
            language: s.en
        });
    },
    onReady: function() {},
    onShow: function() {
        this.data.id && 0 != this.data.id && (this.search(), this.getData(), this.getCartList());
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {
        this.data.isLoadMore ? this.getMoreData() : console.log("没有更多数据");
    },
    onShareAppMessage: function() {
        return {
            path: "/packageE/others/fixedPrice/fixedPrice?id=" + this.data.id
        };
    },
    toMember: function() {
        wx.reLaunch({
            url: "/packageG/member_v2/member_v2"
        });
    },
    changeIcon: function() {
        this.setData({
            icon: !0
        });
    },
    closeIcon: function() {
        this.setData({
            icon: !1
        });
    },
    openGoodsDetail: function(t) {
        var a = t.currentTarget.dataset.item;
        a.id == this.data.hand_goods_id ? this.setData({
            goodsPopupShow: !0
        }) : (this.data.hand_goods_id = a.id, this.getGoodsDetails(a.id, "goodsDetails"));
    },
    getGoodsDetails: function(e, s) {
        var o = this, i = a.getNetAddresss("goods.goods.get-goods-page");
        a._postNetWork({
            url: i,
            data: {
                id: Number(e)
            },
            success: function(a) {
                var e, i = a.data;
                if (1 !== i.result) return o.tips(i.msg);
                var d = "";
                "goodsDetails" == s ? d = "goodsPopupShow" : "specs" == s && (d = "specsShow"), 
                i.data.get_goods.end_time = o.data.time, o.setData((t(e = {}, d, !0), t(e, "hand_goods_obj", i.data.get_goods), 
                e));
            }
        });
    },
    close_yz_specs_popup: function(t) {
        console.log(t), 1 == t.detail.clicktype ? (this.addCartRequest({
            goods: {
                id: t.detail.goodsId
            },
            cartid: t.detail.cartid,
            total: t.detail.goodsCount,
            optionsId: t.detail.optionsId
        }), this.setData({
            specsShow: !1
        })) : this.setData({
            specsShow: !1
        });
    },
    openSpecs: function(t) {
        console.log(this.data.specsShow);
        var a = t.currentTarget.dataset.item;
        a.id == this.data.hand_goods_id ? this.setData({
            specsShow: !0
        }) : (this.data.hand_goods_id = a.id, this.getGoodsDetails(a.id, "specs"));
    },
    calculateTotal: function(t) {
        var a = {};
        for (var e in t) {
            var s = t[e].goods_id;
            if (this.data.cart_ids.push(t[e].id), a[s]) {
                var o = a[s].goods_id;
                a[s].goods_id = o, a[s].total = a[s].total + t[e].total;
            } else a[s] = {}, a[s].goods_id = t[e].goods_id, a[s].total = t[e].total, a[s].id = t[e].id;
        }
        this.setData({
            goodsCartsTotal: a
        });
    },
    getFirstCart: function(t) {
        this.data.carts = t;
        var a = 0, e = 0;
        this.data.carts.forEach(function(t) {
            a += t.total, e += t.total * t.goods.price;
        }), e = parseFloat(e).toFixed(2), this.setData({
            cartsNum: a,
            cartsTotal: e
        }), this.calculateTotal(this.data.carts);
    },
    openCartListPopup: function() {
        this.setData({
            cartListShow: !this.data.cartListShow
        });
    },
    cartListAdd: function(t) {
        console.log(t.detail), console.log(this.data.goodsCartsTotal), this.addCartRequest({
            goods: t.detail.goods,
            cartid: t.detail.id,
            total: t.detail.total + 1
        });
    },
    cartListReduce: function(t) {
        console.log(t.detail), this.reduceCartRequest({
            goods: t.detail.goods,
            cartid: t.detail.id,
            total: t.detail.total - 1
        });
    },
    close_yz_cartList_popup: function() {
        this.setData({
            cartListShow: !1
        });
    },
    submitGoods: function() {
        if (this.data.showChoose && !this.data.radioChoose) return this.tips("请先选择配送方式");
        if (this.data.showChoose) {
            this.data.cart_ids = [];
            for (var t = 0; t < this.data.dispatch_types[this.data.radioChoose].member_carts.length; t++) this.data.cart_ids.push(this.data.dispatch_types[this.data.radioChoose].member_carts[t].id);
        }
        wx.navigateTo({
            url: "/packageD/buy/myOrder_v2/myOrder_v2?tag=-10&cart_ids=" + JSON.stringify(this.data.cart_ids)
        });
    },
    checkGoods: function() {
        var t = this;
        if (this.data.cartsNum <= 0) this.tips("去选择商品"); else {
            var e = a.getNetAddresss("plugin.pack-fixed-price.api.checkout.index");
            a._getNetWork({
                url: e,
                data: {
                    cart_ids: this.data.cart_ids.join(",")
                },
                success: function(a) {
                    var e = a.data;
                    if (1 !== e.result) return t.tips(e.msg);
                    1 == e.data.need_choose ? t.setData({
                        dispatch_types: e.data.dispatch_types,
                        showChoose: !0
                    }) : t.submitGoods();
                }
            });
        }
    },
    getCartList: function() {
        var t = this, e = a.getNetAddresss("plugin.pack-fixed-price.api.member-cart.index");
        a._postNetWork({
            url: e,
            data: {
                id: this.data.id
            },
            success: function(a) {
                var e = a.data;
                if (1 !== e.result) return t.tips(e.msg);
                var s = e.data;
                t.setData({
                    carList: s
                }), t.getFirstCart(s), console.log(s);
            }
        });
    },
    addCartNum: function(t) {
        var a = t.currentTarget.dataset.item, e = t.currentTarget.dataset.cartid;
        console.log(a), this.addCartRequest({
            goods: a,
            cartid: e
        });
    },
    addCartRequest: function(t) {
        var e = this, s = t.goods, o = t.cartid, i = t.total, d = t.optionsId;
        if (console.log("加入购物车"), s.vip_level_status && 1 == s.vip_level_status.status) return this.tips(s.vip_level_status.tips);
        if (0 == s.stock) return this.tips("库存为零");
        var n = 1, r = "", c = {};
        if (a._isTextEmpty(this.data.goodsCartsTotal[s.id]) || 0 == o) r = a.getNetAddresss("plugin.pack-fixed-price.api.member-cart.store"), 
        i && i > 0 && (n = i), c = {
            goods_id: s.id,
            total: n,
            id: this.data.id
        }, d && d > 0 && (c.option_id = d); else {
            if (this.data.goodsCartsTotal[s.id].total >= s.stock) return this.tips("库存不足");
            var h = this.data.goodsCartsTotal;
            n = Number(h[s.id].total) + 1, i && i > 0 && (n = i), this.data.goodsCartsTotal[s.id].total = n, 
            r = a.getNetAddresss("plugin.pack-fixed-price.api.member-cart.update-num"), c = {
                id: o,
                num: n
            };
        }
        a._getNetWork({
            url: r,
            data: c,
            success: function(t) {
                var a = t.data;
                e.tips(a.msg), 1 == a.result && e.getCartList();
            }
        });
    },
    tips: function(t) {
        return wx.showToast({
            title: t,
            icon: "none"
        }), !1;
    },
    previewImage: function() {
        wx.showToast({
            title: "长按分享好友",
            icon: "none",
            duration: 1800
        }), wx.previewImage({
            urls: [ this.data.info.applets_poster ]
        });
    },
    saveImg: function() {
        wx.getSetting({
            success: function(t) {
                t.authSetting["scope.writePhotosAlbum"] || wx.authorize({
                    scope: "scope.writePhotosAlbum",
                    success: function() {
                        console.log("授权成功");
                    }
                });
            }
        });
        var t = this.data.info.applets_poster;
        wx.downloadFile({
            url: t,
            success: function(t) {
                console.log(t), wx.saveImageToPhotosAlbum({
                    filePath: t.tempFilePath,
                    success: function(t) {
                        wx.showToast({
                            title: "保存成功",
                            icon: "none",
                            duration: 2e3
                        });
                    },
                    fail: function(t) {
                        console.log(t), "saveImageToPhotosAlbum:fail auth deny" === t.errMsg && (console.log("当初用户拒绝，再次发起授权"), 
                        wx.openSetting({
                            success: function(t) {
                                t.authSetting["scope.writePhotosAlbum"] ? wx.showToast({
                                    icon: "none",
                                    title: "获取权限成功,再次长按保存图片",
                                    duration: 1500
                                }) : wx.showToast({
                                    icon: "none",
                                    title: "获取权限失败,保存图片需要用户授权",
                                    duration: 1500
                                });
                            }
                        }));
                    },
                    complete: function(t) {
                        console.log(t);
                    }
                });
            }
        });
    },
    copy: function() {
        wx.setClipboardData({
            data: this.data.info.applets_url,
            success: function(t) {
                wx.showToast({
                    title: "复制成功"
                });
            }
        }), this.setData({
            share: !1
        });
    },
    closeposter: function() {
        this.setData({
            poster: !1
        });
    },
    openposter: function() {
        this.setData({
            poster: !0,
            share: !1
        });
    },
    close: function() {
        this.setData({
            show: !1
        });
    },
    closeShare: function() {
        this.setData({
            share: !1
        });
    },
    closeTip: function() {
        this.setData({
            tipShow: !1
        });
    },
    openTip: function() {
        this.setData({
            tipShow: !0
        });
    },
    openShare: function() {
        this.setData({
            share: !0
        });
    },
    open: function() {
        this.setData({
            show: !0
        });
    },
    change: function(t) {
        return t < 10 ? "0" + t : t;
    },
    timestampToTime: function(t) {
        var a = new Date(1e3 * t);
        return a.getFullYear() + "/" + ((a.getMonth() + 1 < 10 ? "0" + (a.getMonth() + 1) : a.getMonth() + 1) + "/") + (this.change(a.getDate()) + " ") + (this.change(a.getHours()) + ":") + this.change(a.getMinutes()) + this.change(a.getSeconds());
    },
    toIndex: function() {
        wx.reLaunch({
            url: "/packageG/index/index"
        });
    },
    getTime: function(t) {
        var a, e, s, o;
        a = Math.floor(t / 1e3 / 60 / 60 / 24), e = Math.floor(t / 1e3 / 60 / 60 % 24), 
        s = Math.floor(t / 1e3 / 60 % 60), o = Math.floor(t / 1e3 % 60);
        var i = {
            day: a,
            hours: this.toDouble(e),
            minutes: this.toDouble(s),
            seconds: this.toDouble(o)
        };
        this.setData({
            rest_time: i
        });
    },
    toDouble: function(t) {
        return t < 10 ? "0" + t : t;
    },
    checktime: function() {
        var t = this, a = this, e = null;
        1e3 * a.data.info.start_at > new Date().getTime() && (a.setData({
            status: "1"
        }), e = setInterval(function() {
            var s = 1e3 * a.data.info.start_at - new Date().getTime();
            s > 0 ? (s--, a.getTime(s)) : (clearInterval(e), t.checktime());
        }, 1e3)), 1e3 * a.data.info.start_at < new Date().getTime() && new Date().getTime() < 1e3 * a.data.info.end_at && (a.setData({
            status: "2",
            time: 1e3 * a.data.info.end_at - new Date().getTime()
        }), e = setInterval(function() {
            var s = 1e3 * a.data.info.end_at - new Date().getTime();
            s > 0 ? (s--, a.getTime(s)) : (clearInterval(e), t.checktime());
        }, 1e3)), new Date().getTime() > 1e3 * a.data.info.end_at && this.setData({
            status: "3"
        });
    },
    goodsDetailCartAdd: function(t) {
        this.setData({
            goodsPopupShow: !1,
            specsShow: !0
        });
    },
    getData: function() {
        var t = this, e = this, s = a.getNetAddresss("plugin.pack-fixed-price.api.activity.get");
        a._getNetWork({
            url: s,
            data: {
                id: this.data.id
            },
            success: function(a) {
                var s = a.data;
                1 == s.result ? (e.setData({
                    info: s.data
                }), e.checktime(), e.setData({
                    "info.start_time": e.timestampToTime(e.data.info.start_at),
                    "info.end_time": e.timestampToTime(e.data.info.end_at)
                })) : (t.setData({
                    status: "3"
                }), wx.showToast({
                    title: s.msg,
                    icon: "none"
                }));
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    search: function(t) {
        var e = this, s = a.getNetAddresss("plugin.pack-fixed-price.api.goods.get-goods-list");
        a._getNetWork({
            url: s,
            data: {
                id: this.data.id
            },
            success: function(t) {
                var a = t.data;
                1 == a.result ? (e.setData({
                    isLoadMore: !0,
                    total_page: a.data.last_page,
                    list: a.data.data
                }), e.data.total_page || e.setData({
                    total_page: 0
                })) : wx.showToast({
                    title: a.msg,
                    icon: "none"
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    reduceCartNum: function(t) {
        var a = t.currentTarget.dataset.item, e = t.currentTarget.dataset.cartid;
        this.reduceCartRequest({
            goods: a,
            cartid: e
        });
    },
    reduceCartRequest: function(t) {
        var e = this, s = t.goods, o = t.cartid, i = t.total, d = s.id, n = 0;
        if (i || 0 === i) n = i; else {
            var r = this.data.goodsCartsTotal;
            n = Number(r[d].total) - 1, this.data.goodsCartsTotal[d].total = n;
        }
        var c = "", h = {};
        n <= 0 ? (c = a.getNetAddresss("plugin.pack-fixed-price.api.member-cart.destroy"), 
        h = {
            ids: o
        }) : (c = a.getNetAddresss("plugin.pack-fixed-price.api.member-cart.update-num"), 
        h = {
            id: o,
            num: n
        }), a._getNetWork({
            url: c,
            data: h,
            success: function(t) {
                var a = t.data;
                e.tips(a.msg), 1 == a.result && e.getCartList();
            }
        });
    },
    getMoreData: function() {
        var t = this;
        if (this.setData({
            isLoadMore: !1
        }), !(this.data.page >= this.data.total_page)) {
            var e = this.data.page + 1;
            this.setData({
                page: e
            });
            var s = a.getNetAddresss("plugin.pack-fixed-price.api.goods.get-goods-list");
            a._getNetWork({
                url: s,
                data: {
                    id: this.data.id,
                    page: this.data.page
                },
                success: function(a) {
                    var e = a.data;
                    if (1 == e.result) {
                        var s = t.data.list.concat(e.data.data);
                        t.setData({
                            list: s
                        });
                    } else {
                        wx.showToast({
                            title: e.msg,
                            icon: "none"
                        });
                        var o = t.data.page - 1;
                        t.setData({
                            page: o,
                            isLoadMore: !1
                        });
                    }
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    }
});